package io.renren.com.qywt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.com.qywt.dao.AftersalePurchasereturnFeeDao;
import io.renren.com.qywt.dto.AftersalePurchasereturnFeeDTO;
import io.renren.com.qywt.entity.AftersaleFinanceCollectEntity;
import io.renren.com.qywt.entity.AftersalePurchasereturnFeeEntity;
import io.renren.com.qywt.excel.AftersaleOrderExcel;
import io.renren.commons.mybatis.service.impl.BaseServiceImpl;
import io.renren.commons.mybatis.service.impl.CrudServiceImpl;
import io.renren.com.qywt.dao.AftersaleOrderDao;
import io.renren.com.qywt.dto.AftersaleOrderDTO;
import io.renren.com.qywt.entity.AftersaleOrderEntity;
import io.renren.com.qywt.service.AftersaleOrderService;
import io.renren.commons.security.context.TenantContext;
import io.renren.commons.security.user.SecurityUser;
import io.renren.commons.tools.utils.ConvertUtils;
import io.renren.commons.tools.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单
 *
 * @author jiaohj 15110414840@139.com
 * @since 3.0 2022-08-05
 */
@Service
public class AftersaleOrderServiceImpl extends CrudServiceImpl<AftersaleOrderDao, AftersaleOrderEntity, AftersaleOrderDTO>  implements AftersaleOrderService {


    @Override
    public QueryWrapper<AftersaleOrderEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<AftersaleOrderEntity> wrapper = new QueryWrapper<>();
        String customerName = (String)params.get("customerName");
        wrapper.eq(StringUtils.isNotBlank(customerName), "customer_name", customerName);
        String orderStatus = (String)params.get("orderStatus");
        wrapper.eq(StringUtils.isNotBlank(orderStatus), "order_status", orderStatus);
        String supplyName = (String)params.get("supplyName");
        wrapper.like(StringUtils.isNotBlank(supplyName), "supply_name", supplyName);
        String deliveryNo = (String)params.get("deliveryNo");
        wrapper.eq(StringUtils.isNotBlank(deliveryNo), "delivery_no", deliveryNo);
        String problemType = (String)params.get("problemType");
        wrapper.eq(StringUtils.isNotBlank(problemType), "problem_type", problemType);
        wrapper.eq("tenant_code", TenantContext.getTenantCode(SecurityUser.getUser()));
        String settleFlag = (String)params.get("settleFlag");
        wrapper.eq(StringUtils.isNotBlank(settleFlag), "settle_flag", settleFlag);
        String startDate=(String)params.get("startDate");
        String endDate=(String)params.get("endDate");
        if(StringUtils.isNotBlank(startDate)&&StringUtils.isNotBlank(endDate)){
            wrapper.lambda().ge(AftersaleOrderEntity::getCreateDate,DateUtils.parse(startDate,DateUtils.DATE_TIME_PATTERN));
            wrapper.lambda().le(AftersaleOrderEntity::getCreateDate,DateUtils.parse(endDate,DateUtils.DATE_TIME_PATTERN));
        }
        wrapper.lambda().orderByDesc(AftersaleOrderEntity::getCreateDate);
        return wrapper;
    }


    @Override
    public void batchImport(List<AftersaleOrderExcel> list) {
        Map<String, Object> params=new HashMap<>();
        List<AftersaleOrderEntity> updateList=new ArrayList<>();
        List<AftersaleOrderEntity> insertList=new ArrayList<>();

        list.stream().forEach(e->{
            params.put("deliveryNo",e.getDeliveryNo());
            AftersaleOrderEntity entity=baseDao.selectOne(this.getWrapper(params));
            if(ObjectUtil.isNotNull(entity)){
                System.out.println("111111111111");
                BeanUtil.copyProperties(e,entity,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                updateList.add(entity);
            }else{
                insertList.add(ConvertUtils.sourceToTarget(e,AftersaleOrderEntity.class));
            }
        });
        this.updateBatchById(updateList);
        this.insertBatch(insertList);
    }
}